import sqlite3
import random
from pyecharts import options as opts
from pyecharts.charts import Sankey
from pyecharts.charts import Sunburst
from pyecharts.globals import CurrentConfig

CurrentConfig.ONLINE_HOST = "../pyecharts-assets/assets/"


dicYJL={'00':'湿地','01':'耕地','02':'园地','03':'林地',
       '04':'草地','05':'商服用地','06':'工矿仓储用地',
       '07':'住宅','08':'公共管理与公共服务用地','09':'特殊用地',
       '10':'交通运输用地','11':'水域及水利设施用地','12':'其他土地'}
dicSDL={'11':'农用地','22':'建设用地','33':'未利用地'}




def getData(sqlStr):
    conn = sqlite3.connect('./report.sqlite')
    cur = conn.cursor()
    cur.execute(sqlStr)
    rows = cur.fetchall()
    cur.close()
    return rows

def makeDic(sqlStr):
    sqlStr="select "
    rows = getData(sqlStr)
    dic = {}
    for row in rows:

        pass



def firstClassFlowSankey():
    # 一级类流向桑基图
    getdataStr="SELECT dlbm19,dlbm20,round(dlmj/10000,2) FROM t_hzmj WHERE length(dlbm19)=2 AND length(dlbm20)=2 ORDER BY dlbm19"
    def getDataFlowData():
        rows = getData(getdataStr)
        nodes = [{"name": dlDic[key]} for key in dicYJL]  # 构造nodes
        for key in dicYJL:
            nodes.append({"name": key + dlDic[key]})
        links = [{"source": dicYJL[row[0]], "target": row[1] + dicYJL[row[1]], "value": row[2]} for row in rows if
                 row[0] != row[1]]
        return nodes, links

    nodes,links=getDataFlowData()
    c = (
        Sankey()
        .add(
            "土地利用一级类流向图",
            nodes,
            links,
            linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=1, color="source"),
            label_opts=opts.LabelOpts(position="right"),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title=""))
        .render("sankey_base.html")
    )


def firstClassFlowSunburst():

    def getDLData():
        rows = getData("SELECT bm193,bm191,dlbm19,mj from v_yejdl")
        #获取的数据按三级地类构建嵌套字典
        dic = {}
        sumArea =0
        for row in rows:
            dl11, dl01, dl0101, mj = row[0], row[1], row[2], row[3]
            sumArea += mj
            if dl11 in dic:
                if dl01 in dic[dl11]:
                    dic[dl11][dl01][dl0101] = mj
                else:
                    dic[dl11][dl01] = {}
                    dic[dl11][dl01][dl0101] = mj
            else:
                dic[dl11] = {}
                dic[dl11][dl01] = {}
                dic[dl11][dl01][dl0101] = mj
        return dic,sumArea

    data = []
    dicDL,sunArea = getDLData()
    for key0 in dicDL:
        children0 = []
        data.append(opts.SunburstItem(name=dicSDL[key0], children=children0))
        for key1 in dicDL[key0]:
            children1 = []
            children0.append(opts.SunburstItem(
                name=dicYJL[key1],
                itemstyle_opts={"color":"#"+''.join(random.sample('0123456789abcdef',6))},
                children=children1))
            for key2 in dicDL[key0][key1]:
                children1.append(opts.SunburstItem(
                    name=key2,
                    label_opts={"position":"outside", "padding":3,"silent":True},
                    itemstyle_opts={"color":"#"+''.join(random.sample('0123456789abcdef',6))},
                    # value="%.2f%%"%(dicDL[key0][key1][key2]/sunArea)
                    value=dicDL[key0][key1][key2]
                ))

    sunburst = (
        Sunburst(init_opts=opts.InitOpts(width="1000px", height="600px"))
            .add(series_name="",
                 data_pair=data,
                 radius=[0, "95%"],
                 levels=[{},
                         {
                             "r0":"15%",
                             "r":"35%"
                          },
                         {
                             "r0":"35%",
                             "r":"70%"},
                         {
                             "r0":"70%",
                             "r":"72%"
                         }])
            .set_global_opts(title_opts=opts.TitleOpts(title="现状地类旭日图"))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
            .render("basic_sunburst.html")
    )


def main():
    # firstClassFlowSankey()
    firstClassFlowSunburst()


if __name__ == '__main__':
    main()

